/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.model;

import framework.view.UI;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;

/**
 *
 * @author ThanhNguyen
 */
public class FinancialCompany extends Observable {

    protected List<Customer> customers;
    protected List<Account> accounts;
    protected Factory factory;

    public Factory getFactory() {
        return factory;
    }
    protected UI ui;

    public UI getUI() {
        return ui;
    }

    public FinancialCompany() {
        customers = new ArrayList<>();
        accounts = new ArrayList<>();
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public void addCustomer(Customer customer) {
        customers.add(customer);
    }

    public void addAccount(Account account) {
        accounts.add(account);
    }

    public void removeCustomer(Customer customer) {
        customers.remove(customer);
    }

    public Customer findCustomer(String name) {
        for (Customer customer : customers) {
            if ((customer).getName().equals(name)) {
                return customer;
            }
        }
        return null;
    }

    public boolean isCustomerExisted(String name) {
        return (findCustomer(name) != null);
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public static void main(String[] args) {
        // TODO code application logic here
        FinancialCompany finco = new FinancialCompany();
        finco.start();
    }

    public void start() {
        factory = new Factory();
        factory.setFinancialCompany(this);
        Factory.setFactory(factory);

        ui = new UI();
        ui.setFinancialCompany(this);
        ui.setVisible(true);
    }

    public void createAccount(HashMap<String, String> fields) {
        Customer cust = findCustomer(fields.get(Constant.A_NAME));
        if (cust == null) {
            cust = factory.createCustomer(fields);
            addCustomer(cust);
        }
        Account account = factory.createAccount(cust, fields);
        cust.addAccount(account);
        addAccount(account);
    }

    public void updateUI() {
        setChanged();
        notifyObservers();
    }

    public void addInterest() {
        for(Customer customer: customers)
        {
            customer.addInterest();
        }
    }
}
